//
//  AXHomeNcpscVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/22.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeNcpscVC.h"
#import "LKCircleScrollView.h"
#import "Header.h"
#import "ZXCategories.h"
#import "UIView+SDAutoLayout.h"
#import "AXHomeNcpscFirstCell.h"
#import "AXHomeNcpsceScondCell.h"
#import "AXHomeNcpscThirdCell.h"
#import "AXHomeNcpscFourthCell.h"
#import "AXHomeNcpscFiveCell.h"
#import "AXHomeQbbbVC.h"
#import "AXYxtyViewController.h"
#import "AXHomeXxtydVC.h"
#import "AXHomeCpsyVC.h"
#import "AXYjsgViewController.h"
#import "AXHdzqVC.h"
#import "AXHomePtsfxVC.h"
#import "CFDetailInfoController.h"



@interface AXHomeNcpscVC ()<UITableViewDelegate,UITableViewDataSource,LKCircleScrollViewDelegate, LKCircleScrollViewDataSource,UISearchBarDelegate>
/** headView */
@property (nonatomic ,strong) UIView *headView;
@property (nonatomic, strong) UITableView *tableView;
/** 广告图片 */
@property (nonatomic ,strong) UIImageView *advImageView;
/** 循环滚动View */
@property (nonatomic, strong) LKCircleScrollView *circleScrollView;
@property (nonatomic, strong) NSArray<NSString *> *scrollViewImageNames;

@end

@implementation AXHomeNcpscVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    
    self.tableView.tableHeaderView = _headView;
    [_headView setNeedsLayout];
    [_headView layoutIfNeeded];//刷新布局
    CGFloat height = [_headView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    _headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, height);//headView 的大小
    _headView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = _headView;
    
}

- (void)creatView {
    
    _headView = [[UIView alloc]init];
    
    //添加scrollview
    self.scrollViewImageNames = @[@"shcm", @"sg2.jpeg.jpeg", @"sg3.jpeg.jpeg"];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView = [[LKCircleScrollView alloc]init];
    self.circleScrollView.isAutoScroll = NO;
    self.circleScrollView.delegate = self;
    self.circleScrollView.dataSource = self;
    [self.headView addSubview:self.circleScrollView];
    
    
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    //注册cell
    [self.tableView registerClass:[AXHomeNcpscFirstCell class] forCellReuseIdentifier:[AXHomeNcpscFirstCell reuseID]];
    [self.tableView registerClass:[AXHomeNcpsceScondCell class] forCellReuseIdentifier:[AXHomeNcpsceScondCell reuseID]];
    [self.tableView registerClass:[AXHomeNcpscThirdCell class] forCellReuseIdentifier:[AXHomeNcpscThirdCell reuseID]];
    [self.tableView registerClass:[AXHomeNcpscFourthCell class] forCellReuseIdentifier:[AXHomeNcpscFourthCell reuseID]];
     [self.tableView registerClass:[AXHomeNcpscFiveCell class] forCellReuseIdentifier:[AXHomeNcpscFiveCell reuseID]];
    _headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 129);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}

- (void)setlayout {
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(Wi)
    .heightIs(Hi-Hs-Htb-44);
    
    self.circleScrollView.sd_layout
    .leftSpaceToView(self.headView,0)
    .rightSpaceToView(self.headView,0)
    .topSpaceToView(self.headView,0)
    .heightIs(129);
    
    [_headView setupAutoHeightWithBottomView:self.circleScrollView bottomMargin:0];
    
}


#pragma mark - tableViewDelegate & tableViewDatasorce
/** cell 的点击方法 */

//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

/** cell的高度 */

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return 100;
        
    }else if (indexPath.section == 1) {
        
        return 93;
        
    }else if (indexPath.section == 2) {
        
        return 130;
    }else if (indexPath.section == 3) {
        
        return 103;
    }
    
    return 140;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        AXHomeNcpscFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNcpscFirstCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.tapButtonClickedBlock = ^(NSInteger index) {
            //全部宝贝、优先体验
            if (index == 0) {
                
                AXHomeQbbbVC *vc = [[ AXHomeQbbbVC alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }else if (index ==1) {
                
                 AXYxtyViewController *vc = [[AXYxtyViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
               
                
            }else if (index == 2){
                
                 AXHomeCpsyVC *vc = [[ AXHomeCpsyVC alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
               
            }
            else if (index == 3){
                
                AXHomeXxtydVC *vc = [[AXHomeXxtydVC alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        };

        return cell;
    }
    /** 应季水果 活动专区*/
    if (indexPath.section == 1) {
        
        AXHomeNcpsceScondCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNcpsceScondCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.leftImgView.tag = 100;
        cell.rightImgView.tag = 101;
    
        cell.tapButtonClickedBlock = ^(NSInteger index) {
            
           
            if (index == 100) {
                
                AXYjsgViewController *vc = [[AXYjsgViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            } else {
                
                AXHdzqVC *vc = [[AXHdzqVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
            
           
            
            
        };
        
        return cell;
    }
    
    //平台示范县
    if (indexPath.section == 2) {
        
        AXHomeNcpscThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNcpscThirdCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.leftImgView.tag = 100;
        cell.rightOneImgView.tag = 101;
        cell.rightTwoImgView.tag = 102;
        
        cell.tapButtonClickedBlock = ^(NSInteger index) {
            
          //平台示范县
            if (index == 100) {
                
                AXHomePtsfxVC *vc = [[AXHomePtsfxVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            //草莓
            else if (index == 101){
                
                CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
                
            }//鲜果
            else {
                
                CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
                
            }
            
        
            
        };
        
        
        return cell;
    }
    
    if (indexPath.section == 3) {
        
        AXHomeNcpscFourthCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNcpscFourthCell reuseID] forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
          cell.tapButtonClickedBlock = ^(NSInteger index) {
              
              CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
              [self.navigationController pushViewController:vc animated:YES];
          };
        
        return cell;
    }
    
    
    
    AXHomeNcpscFiveCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeNcpscFiveCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.tapButtonClickedBlock = ^(NSInteger index) {
        //全部宝贝、优先体验
        if (index == 0) {
            
            CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (index ==1) {
            
            CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
            
        }else if (index == 2){
            
            CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        else if (index == 3){
            
            CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
    };
    
    return cell;
    
    
    
}



#pragma mark - Circle Scroll View Data Source & Delegate

- (NSInteger)numberOfViewsInCircleScrollView:(LKCircleScrollView *)circleScrollView {
    
    return self.scrollViewImageNames.count;
}

/** 视图来源 */
- (UIView *)circleScrollView:(LKCircleScrollView *)circleScrollView viewAtIndex:(NSInteger)index {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.scrollViewImageNames[index]]];
    imageView.contentMode =UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds=YES;//
    
    if (index == 0) {
        
        imageView.userInteractionEnabled = YES;
        
        
        
    }
    
    
    return imageView;
}

/** 点击View时调用  */
- (void)circleScrollView:(LKCircleScrollView *)circleScrollView didTapViewAtIndex:(NSInteger)index {
    if (index == 0) {
        //事件不会触发，点击事件可能被ScrollView截获
        //        ZXVoteProductionListViewController *vc = [[ZXVoteProductionListViewController alloc] init];
        //        [self.navigationController pushViewController:vc animated:YES];
    }
}





- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
}


#pragma mark - UISearchBar delegate
//将要开始编辑时的回调，返回为NO，则不能编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
    return NO;
}


@end



